1
2
3
4 package uk.ac.roe.antigen.utils;
5
6 import java.io.File;
7 import java.io.FileInputStream;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10 import java.net.URI;
11 import java.nio.channels.FileChannel;
12 import java.util.logging.Logger;
13
14 /***
15 * A file that is able to be copied - you'd think this would be straightforward
16 * wouldn't you?
17 *
18 * @author jdt
19 */
20 public class CopyableFile extends File {
21 /***
22 * Logger for this class
23 */
24 private static final Logger logger = Logger.getLogger(CopyableFile.class.getName());
25
26 public static void main(String[] args) throws IOException {
27
28 CopyableFile file = new CopyableFile("C:/cygwin");
29 file.copyTo(new File("C:/cygwin2"));
30
31 System.out.println("is abs "+file.isAbsolute());
32 System.out.println("absolute file " + file.getAbsoluteFile());
33 System.out.println("absolute path "+file.getAbsolutePath());
34 System.out.println("canonical file "+file.getCanonicalFile());
35 System.out.println("canonical path "+ file.getCanonicalPath());
36 System.out.println("name "+file.getName());
37 System.out.println("parent "+file.getParent());
38 System.out.println("parent file "+file.getParentFile());
39 System.out.println("path "+file.getPath());
40
41
42 }
43
44
45
46 /***
47 * Copy this file or directory to the given new location
48 * @param newLocation
49 * @throws IOException
50 */
51 public void copyTo(File newLocation) throws IOException {
52 logger.fine("Copying "+getAbsolutePath()+" to "+newLocation);
53 if (isFile()) {
54 logger.fine("(file)");
55 copyFileTo(newLocation);
56 } else {
57 logger.fine("(folder)");
58 File[] files = listFiles();
59 logger.fine("Contains "+files.length+" files");
60 newLocation.mkdirs();
61 for (int i = 0;i<files.length;++i) {
62 logger.fine("Processing " +files[i]);
63 CopyableFile file = new CopyableFile(files[i]);
64 File newFile = new File(newLocation,file.getName());
65 file.copyTo(newFile);
66 }
67 }
68
69 }
70
71 /***
72 * You'd think this would be obvious....
73 * Remove a directory, even if not empty
74 */
75 public boolean recursivelyDelete() {
76 if (isFile()) {
77 logger.fine("deleting "+getName());
78 return delete();
79 } else {
80 File[] files = listFiles();
81 for (int i = 0;i<files.length;++i) {
82 CopyableFile file = new CopyableFile(files[i]);
83 file.recursivelyDelete();
84 }
85 return delete();
86 }
87
88 }
89
90 private void copyFileTo(File destination) throws IOException {
91 logger.fine("Copying from "+destination+"...");
92 FileChannel srcChannel = new FileInputStream(getAbsolutePath()).getChannel();
93 logger.fine("...got source channel "+srcChannel+"...");
94 FileChannel destChannel = new FileOutputStream(new File(destination.getAbsolutePath())).getChannel();
95 logger.fine("...got destination channel "+destChannel+"...");
96 logger.fine("...Got channels...");
97 destChannel.transferFrom(srcChannel, 0, srcChannel.size());
98 logger.fine("...transferred.");
99 srcChannel.close();
100 destChannel.close();
101 }
102
103 /***
104 * @param arg0
105 */
106 public CopyableFile(String arg0) {
107 super(arg0);
108 }
109
110 /***
111 * @param arg0
112 * @param arg1
113 */
114 public CopyableFile(File arg0, String arg1) {
115 super(arg0, arg1);
116 }
117
118 public CopyableFile(File arg0) {
119 super(arg0.getAbsolutePath());
120 }
121
122
123 /***
124 * @param arg0
125 * @param arg1
126 */
127 public CopyableFile(String arg0, String arg1) {
128 super(arg0, arg1);
129 }
130 /***
131 * @param arg0
132 */
133 public CopyableFile(URI arg0) {
134 super(arg0);
135 }
136 }